package com.hp.bon.sgw.stdbss30.bizSlotOutQuery;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.stdbss30.BizSlot;
import com.hp.bon.sgw.stdbss30.utils.HttpClientUtils;
import com.hp.bon.sgw.stdbss30.common.BizConsts;
import com.hp.bon.sgw.stdbss30.common.BizParam;
import com.hp.bon.sgw.stdbss30.common.BizResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BillDetailQueryYXHB9BizSlot implements BizSlot {
	private static final Logger _log = LoggerFactory.getLogger(BillDetailQueryYXHB9BizSlot.class);
	@Override
	public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
		BizResult bizResult = new BizResult();

		try {
			String queryFlag = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Bill_Information/Query_Flag");
			String billingCycle = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Bill_Information/Billing_Cycle");
			String destinationAttr = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Bill_Information/DestinationAttr");
			String accNbr = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Bill_Information/Destination_Id");

			JSONObject operAttrStruct = new JSONObject();
			operAttrStruct.put("operServiceId", "-1");
			operAttrStruct.put("lanId", "8420100");
			operAttrStruct.put("operPost", "0");
			operAttrStruct.put("operOrgId", "0");
			operAttrStruct.put("staffId", "0");
			operAttrStruct.put("operTime", "0");

			//构建请求条件
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("operAttrStruct", operAttrStruct);
			jsonObject.put("queryFlag", queryFlag);
			jsonObject.put("billingCycle", billingCycle);
			jsonObject.put("destinationAttr", destinationAttr);
			jsonObject.put("accNbr", accNbr);

			StringBuilder msgBodyStr = new StringBuilder();

			//发送请求
			String webserviceUrl = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);
			msgBodyStr.append(jsonObject.toJSONString());

			_log.info("request body is {}", msgBodyStr.toString());
			_log.info("request body is {}", msgBodyStr.toString());
			String respStr = HttpClientUtils.postHttpStr(msgBodyStr.toString(), webserviceUrl);
			_log.info("response is " + respStr.toString());

			if (respStr.equals("")) {
				bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
				return bizResult;
			}

			//解析json 拼接报文
			JSONObject json = JSON.parseObject(respStr);
			StringBuffer buffer = new StringBuffer();
			jsonToXmlStr(xmlMessage, json, buffer);


			bizResult.setHttpBody(buffer.toString());
		} catch (Exception e) {
			bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
			_log.info(e.toString());
		}
		return bizResult;
	}

	public static void jsonToXmlStr(XmlMessage xmlMessage, JSONObject json, StringBuffer buffer) {
		buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		buffer.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">");
		buffer.append("<SOAP-ENV:Header/>");
		buffer.append("<SOAP-ENV:Body>");
		buffer.append("<ns3:SgwQueryAck xmlns:ns3=\"http://www.chinatelecom.cn/SGW/SOAP\">");
		buffer.append("<MID>" + xmlMessage.getMID() + "</MID>");
		buffer.append("<REPTAG>" + xmlMessage.getREPTAG() + "</REPTAG>");
		buffer.append("<RTT>" + xmlMessage.getRTT() + "</RTT>");
		buffer.append("<SIG>" + xmlMessage.getSIG() + "</SIG>");
		buffer.append("<RC>" + xmlMessage.getRC() + "</RC>");
		buffer.append("<DS>" + xmlMessage.getDS() + "</DS>");
		buffer.append("<DATA><![CDATA[<Service_Information>");

		buffer.append("<Result>" + (json.get("resultCode") == null ? "" : json.get("resultCode")) + "</Result>");
		buffer.append("<Acct_ID>" + (json.get("acctId") == null ? "" : json.get("acctId")) + "</Acct_ID>");
		buffer.append("<Total_Charge>" + (json.get("sumCharge") == null ? "" : json.get("sumCharge")) + "</Total_Charge>");
		buffer.append("<Charge_Payed>" + (json.get("chargePayed") == null ? "" : json.get("chargePayed")) + "</Charge_Payed>");

		String entryListStr = String.valueOf(json.get("itemInformationList"));
		JSONArray jsonArray = JSONArray.parseArray(entryListStr);
		if (!entryListStr.equals("null")) {
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject entry = (JSONObject) jsonArray.get(i);
				buffer.append("<Item_Information>");
				buffer.append("<Show_level>" + (entry.get("showLevel") == null ? "" : entry.get("showLevel")) + "</Show_level>");
				buffer.append("<Class_Id>" + (entry.get("classId") == null ? "" : entry.get("classId")) + "</Class_Id>");
				buffer.append("<Parent_Class_Id>" + (entry.get("parentClassId") == null ? "" : entry.get("parentClassId")) + "</Parent_Class_Id>");
				buffer.append("<Charge_type_Name>" + (entry.get("chargeTypeName") == null ? "" : entry.get("chargeTypeName")) + "</Charge_type_Name>");
				buffer.append("<Charge>" + (entry.get("charge") == null ? "" : entry.get("charge")) + "</Charge>");
				buffer.append("<serv_id>" + (entry.get("servId") == null ? "" : entry.get("servId")) + "</serv_id>");
				buffer.append("<Destination_Id>" + (entry.get("destinationId") == null ? "" : entry.get("destinationId")) + "</Destination_Id>");
				buffer.append("<SERVICE_TYPE_NAME>" + (entry.get("serviceTypeName") == null ? "" : entry.get("serviceTypeName")) + "</SERVICE_TYPE_NAME>");
				buffer.append("<ChargeShouldPayed>" + (json.get("chargeShouldPayed") == null ? "" : json.get("chargeShouldPayed")) + "</ChargeShouldPayed>");
				buffer.append("<ProdOfferInstanceId>" + (entry.get("prodOfferInstanceId") == null ? "" : entry.get("prodOfferInstanceId")) + "</ProdOfferInstanceId>");
				buffer.append("<Billing_Cycle>" + (json.get("billingCycleId") == null ? "" : json.get("billingCycleId")) + "</Billing_Cycle>");
				buffer.append("<Acct_Item_Type_Id>" + (entry.get("acctItemTypeId") == null ? "" : entry.get("acctItemTypeId")) + "</Acct_Item_Type_Id>");
				buffer.append("<item_source_flag>" + (entry.get("itemSourceFlag") == null ? "" : entry.get("itemSourceFlag")) + "</item_source_flag>");
				buffer.append("<Product_Offer_Id>" + (entry.get("productOfferId") == null ? "" : entry.get("productOfferId")) + "</Product_Offer_Id>");
				buffer.append("<PaymentFlag>" + (entry.get("paymentFlag") == null ? "" : entry.get("paymentFlag")) + "</PaymentFlag>");
				buffer.append("</Item_Information>");
			}
		}
		buffer.append("</Service_Information>]]></DATA>");
		buffer.append("</ns3:SgwQueryAck>");
		buffer.append("</SOAP-ENV:Body>");
		buffer.append("</SOAP-ENV:Envelope>");
	}

}
